home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / ROM_Kernel_Manuals / Lib_examples / updatestrgad.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-20  |  6.0 KB  |  195 lines

  1. ;/* updatestrgad.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfistq -v -y -j73 updatestrgad.c
  3. Blink FROM LIB:c.o,updatestrgad.o TO updatestrgad LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5. **
  6. ** The values of a string gadget may be updated by removing the gadget,
  7. ** modifying the information in the StringInfo structure, adding the
  8. ** gadget back and refreshing its imagery.
  9. **
  10. ** updatestrgad.c - Show the use of a string gadget.  Shows both the use
  11. ** of ActivateGadget() and how to properly modify the contents of a string
  12. ** gadget.
  13. */
  14. #define INTUI_V36_NAMES_ONLY
  15.  
  16. #include <exec/types.h>
  17. #include <intuition/intuition.h>
  18. #include <intuition/intuitionbase.h>
  19.  
  20. #include <clib/exec_protos.h>
  21. #include <clib/dos_protos.h>
  22. #include <clib/intuition_protos.h>
  23.  
  24. #include <string.h>
  25. #include <stdio.h>
  26.  
  27. #ifdef LATTICE
  28. int CXBRK(void)    { return(0); }  /* Disable Lattice CTRL/C handling */
  29. int chkabort(void) { return(0); }  /* really */
  30. #endif
  31.  
  32. /* our function prototypes */
  33. VOID updateStrGad(struct Window *win, struct Gadget *gad, UBYTE *newstr);
  34. VOID handleWindow(struct Window *win, struct Gadget *gad);
  35.  
  36. struct Library *IntuitionBase;
  37.  
  38. /* NOTE that the use of constant size and positioning values are
  39. ** not recommended; it just makes it easy to show what is going on.
  40. ** The position of the gadget should be dynamically adjusted depending
  41. ** on the height of the font in the title bar of the window.  This
  42. ** example adapts the gadget height to the screen font. Alternately,
  43. ** you could specify your font under V37 with the StringExtend structure.
  44. */
  45. #define BUFSIZE (100)
  46. #define MYSTRGADWIDTH (200)
  47. #define MYSTRGADHEIGHT (8)
  48.  
  49. UWORD strBorderData[] =
  50.     {
  51.     0,0, MYSTRGADWIDTH + 3,0, MYSTRGADWIDTH + 3,MYSTRGADHEIGHT + 3,
  52.     0,MYSTRGADHEIGHT + 3, 0,0,
  53.     };
  54. struct Border strBorder =
  55.     {
  56.     -2,-2,1,0,JAM1,5,strBorderData,NULL,
  57.     };
  58. UBYTE strBuffer[BUFSIZE];
  59. UBYTE strUndoBuffer[BUFSIZE];
  60. struct StringInfo strInfo =
  61.     {
  62.     strBuffer,strUndoBuffer,0,BUFSIZE, /* compiler sets remaining fields to zero */
  63.     };
  64. struct Gadget strGad =
  65.     {
  66.     NULL, 20,20,MYSTRGADWIDTH,MYSTRGADHEIGHT,
  67.     GFLG_GADGHCOMP, GACT_RELVERIFY | GACT_STRINGCENTER,
  68.     GTYP_STRGADGET, &strBorder, NULL, NULL,0,&strInfo,0,NULL,
  69.     };
  70.  
  71. #define ANSCNT 4
  72. UBYTE *answers[ANSCNT] = {"Try again","Sorry","Perhaps","A Winner"};
  73. int ansnum = 0;
  74. UBYTE *activated_txt = "Activated";
  75.  
  76. /*   main - show the use of a string gadget.
  77. */
  78. VOID main(int argc, char **argv)
  79. {
  80. struct Window *win;
  81.  
  82. /* make sure to get intuition version 37, for OpenWindowTags() */
  83. IntuitionBase = OpenLibrary("intuition.library", 37);
  84. if (IntuitionBase)
  85.     {
  86.     /* Load a value into the string gadget buffer.
  87.     ** This will be displayed when the gadget is first created.
  88.     */
  89.     strcpy(strBuffer, "START");
  90.  
  91.     if (win = OpenWindowTags(NULL,
  92.                             WA_Width, 400,
  93.                             WA_Height, 100,
  94.                             WA_Title,"Activate Window, Enter Text",
  95.                             WA_Gadgets, &strGad,
  96.                             WA_CloseGadget, TRUE,
  97.                             WA_IDCMP, IDCMP_ACTIVEWINDOW |
  98.                                 IDCMP_CLOSEWINDOW | IDCMP_GADGETUP,
  99.                             TAG_END))
  100.         {
  101.         handleWindow(win,&strGad);
  102.  
  103.         CloseWindow(win);
  104.         }
  105.     CloseLibrary(IntuitionBase);
  106.     }
  107. }
  108.  
  109.  
  110.  
  111. /*
  112. ** Process messages received by the window.  Quit when the close gadget
  113. ** is selected, activate the gadget when the window becomes active.
  114. */
  115. VOID handleWindow(struct Window *win, struct Gadget *gad)
  116. {
  117. struct IntuiMessage *msg;
  118. struct Gadget *gadget;
  119. ULONG  class;
  120.  
  121. for (;;)
  122.     {
  123.     Wait(1L << win->UserPort->mp_SigBit);
  124.     while (msg = (struct IntuiMessage *)GetMsg(win->UserPort))
  125.         {
  126.         /* Stash message contents and reply, important when message
  127.         ** triggers some lengthy processing
  128.         */
  129.         class = msg->Class;
  130.     /* If it's a gadget message, IAddress points to Gadget */
  131.         if((class == IDCMP_GADGETUP)||(class == IDCMP_GADGETDOWN))
  132.                 gadget = (struct Gadget *)msg->IAddress;
  133.         ReplyMsg((struct Message *)msg);
  134.  
  135.         switch (class)
  136.             {
  137.             case IDCMP_ACTIVEWINDOW:
  138.                 /* activate the string gadget.  This is how to activate a
  139.                 ** string gadget in a new window--wait for the window to
  140.                 ** become active by waiting for the IDCMP_ACTIVEWINDOW
  141.                 ** event, then activate the gadget.  Here we report on
  142.                 ** the success or failure.
  143.                 */
  144.                 if(ActivateGadget(gad,win,NULL))
  145.                     updateStrGad(win,gad,activated_txt);
  146.                 break;
  147.             case IDCMP_CLOSEWINDOW:
  148.                 /* here is the way out of the loop and the routine.
  149.                 ** be sure that the message was replied...
  150.                 */
  151.                 return;
  152.                 break;
  153.             case IDCMP_GADGETUP:
  154.                 /* If user hit RETURN in our string gadget for demonstration,
  155.                 ** we will change what he entered.  We only have 1 gadget,
  156.                 ** so we don't have to check which gadget.
  157.                 */
  158.                 updateStrGad(win, &strGad, answers[ansnum]);
  159.         if(++ansnum > ANSCNT) ansnum = 0;  /* point to next answer */
  160.                 break;
  161.             }
  162.         }
  163.     }
  164. }
  165.  
  166.  
  167.  
  168. /*
  169. ** Routine to update the value in the string gadget's buffer, then
  170. ** activate the gadget.
  171. */
  172. VOID updateStrGad(struct Window *win, struct Gadget *gad, UBYTE *newstr)
  173. {
  174. /* first, remove the gadget from the window.  this must be done before
  175. ** modifying any part of the gadget!!!
  176. */
  177. RemoveGList(win,gad,1);
  178.  
  179. /* For fun, change the value in the buffer, as well as the cursor and
  180. ** initial display position.
  181. */
  182. strcpy(((struct StringInfo *)(gad->SpecialInfo))->Buffer, newstr);
  183. ((struct StringInfo *)(gad->SpecialInfo))->BufferPos = 0;
  184. ((struct StringInfo *)(gad->SpecialInfo))->DispPos   = 0;
  185.  
  186. /* Add the gadget back, placing it at the end of the list (~0)
  187. ** and refresh its imagery.
  188. */
  189. AddGList(win,gad,~0,1,NULL);
  190. RefreshGList(gad,win,NULL,1);
  191.  
  192. /* Activate the string gadget */
  193. ActivateGadget(gad,win,NULL);
  194. }
  195.